package com.lottery.material.service.admin.conf.impl;

import com.lottery.material.dao.conf.UserLevelDictMapper;
import com.lottery.material.pojo.conf.UserLevelDict;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojocustom.conf.UserLevelDictCustom;
import com.lottery.material.service.admin.conf.UserLevelDictService;
import com.lottery.material.util.AssertUtil;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：用户等级字典服务接口实现类
 * Created by 朱正磊 on 2019-05-17 10:36
 */
@Service("adminUserLevelDictService")
public class UserLevelDictServiceImpl implements UserLevelDictService {

    private final UserLevelDictMapper userLevelDictMapper;

    @Autowired
    public UserLevelDictServiceImpl(UserLevelDictMapper userLevelDictMapper) {
        this.userLevelDictMapper = userLevelDictMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param userLevelDict 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(UserLevelDict userLevelDict) {
        return userLevelDictMapper.insertSelective(userLevelDict);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return userLevelDictMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public UserLevelDict findById(Integer id) {
        return userLevelDictMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param userLevelDict 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(UserLevelDict userLevelDict) {
        return userLevelDictMapper.updateByPrimaryKeySelective(userLevelDict);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<UserLevelDictCustom> findCustomListPage(Page page) {
        List<UserLevelDictCustom> list = userLevelDictMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public UserLevelDictCustom findCustomById(Integer id) {
        UserLevelDictCustom custom = userLevelDictMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(UserLevelDictCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-05-17 16:00
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add() {
        UserLevelDict userLevelDict = new UserLevelDict();
        userLevelDict.setCreateTime(new Date());
        Integer userLevel = 1;
        Integer maxLevel = userLevelDictMapper.selectMaxLevel();
        if (AssertUtil.isNotEmpty(maxLevel)) {//新增的不是第一条数据
            userLevel = maxLevel + 1;
        }
        userLevelDict.setUserLevel(userLevel);
        userLevelDict.setUserLevelName("V" + userLevel);
        return userLevelDictMapper.insertSelective(userLevelDict);
    }

    /**
     * description：查询所有的数据
     * user 朱正磊
     * time 2019-05-17 16:28
     * @return 返回所有的数据
     */
    @Override
    public List<UserLevelDict> findAll() {
        return userLevelDictMapper.findAll();
    }

    /**
     * description：根据用户等级查询唯一一条数据
     * user 朱正磊
     * time 2019-05-20 10:48
     * @param userLevel 用户等级
     * @return 返回唯一一条数据
     */
    @Override
    public UserLevelDict findByUserLevel(Integer userLevel) {
        return userLevelDictMapper.findByUserLevel(userLevel);
    }

}
